#include "RingQueue.hpp"

using namespace std;

class ThreadData
{
public:
    string _name;
    RingQueue<int> *_rq;
};
void *consumer_run(void *args)
{
    ThreadData *td = static_cast<ThreadData *>(args);
    while (1)
    {

        int data;
        td->_rq->Pop(&data);
        cout << td->_name << ":消费了一个数据 data: " << data << endl;
    }
}
int data = 0;
void *producer_run(void *args)
{
    ThreadData *td = static_cast<ThreadData *>(args);
    while (1)
    {

        sleep(2);
        cout << td->_name << ":生产了一个数据 data: " << data << endl;
        td->_rq->EnQueue(data);
        data++;
    }
}

int main()
{
    RingQueue<int> *rq = new RingQueue<int>();
    pthread_t producer[3], consumer[2];
    ThreadData *thread1 = new ThreadData();
    thread1->_name = "p_thread-1";
    thread1->_rq = rq;
    pthread_create(&producer[0], nullptr, producer_run, thread1);

    ThreadData *thread2 = new ThreadData();
    thread2->_name = "p_thread-2";
    thread2->_rq = rq;
    pthread_create(&producer[1], nullptr, producer_run, thread2);

    ThreadData *thread3 = new ThreadData();
    thread3->_name = "p_thread-3";
    thread3->_rq = rq;
    pthread_create(&producer[2], nullptr, producer_run, thread3);

    ThreadData *thread4 = new ThreadData();
    thread4->_name = "c_thread-1";
    thread4->_rq = rq;
    pthread_create(&consumer[0], nullptr, consumer_run, thread4);

    ThreadData *thread5 = new ThreadData();
    thread5->_name = "c_thread-2";
    thread5->_rq = rq;
    pthread_create(&consumer[1], nullptr, consumer_run, thread5);

    pthread_join(producer[0], nullptr);
    pthread_join(producer[1], nullptr);
    pthread_join(producer[2], nullptr);

    pthread_join(consumer[0], nullptr);
    pthread_join(consumer[1], nullptr);

    return 0;
}